$(document).ready(function () {

    /*this function sets height to background image wrapper so that unnecessary background image part is hidden.*/
	$("#background_img_wrap").height($("#wrapper").height());

    /*function which updates background image wrapper height upon window resize*/
	$(window).resize(function () {
        $("#background_img_wrap").height($("#wrapper").height());
		searchBoxRight();
    });

    /*function which hides background changer container to the left upon images loaded. */
	$(window).load(function () {
        $("#bg_changer").css("left", -10 - $("#bg_changer").width());
    });

    /*function gives style to select element in contact page*/
	$("#select1").sSelect();

    /*function gives style to scrollbars in about us page */
	$('.scrollPane').jScrollPane();

    /*activates tooltip plugin on elements with class .tooltip */
	$('.tooltip').tooltip({
        showURL: false
    });

	/*these functions hide all paragraphs inside boxes and leaves only first of them open. and hide some part of the image.*/
	$(".homeContentBox").addClass("closed");
    $(".homeContentBox .grey_box p").hide();
    $(".homeContentBox .grey_box").each(function () {
        $(this).find('p:first').show();
    });
	/*function which slides up the content of home boxes and hides them. */
    $(".homeContentBox .toggleBox").click(function () {
        $(this).parents(".homeContentBox").toggleClass("closed");
        if ($(this).parents(".homeContentBox").hasClass("closed")) {
            $(this).parents(".homeContentBox").find('p').hide();
            $(this).parents(".homeContentBox").find('p:first').show();
        } else {
            $(this).parents(".homeContentBox").find('p').slideDown();
        }
        return false; 
    });
	
	/*function which hides and shows the models sub menu */
	/*sifr rollback is used to rollback flash and apply sifr again */
    $('#primary-nav li').hover(function () {
        if ($(this).find(".scroll-pane").length > 0) {
            clearTimeout($(this).data('timeout'));
            sIFR.rollback("#primary-nav ul li#li-models > span");
            sIFR.rollback("#primary-nav ul li#li-models > p");
            primaryNavSifrAOver();
            primaryNavSifrPOver();
            $(this).addClass("active");
            $(this).find(".scroll-pane").show();
        }
    }, function () {
        if ($(this).find(".scroll-pane").length > 0) {
            var myobj = $(this);
            var t = setTimeout(function () {
                sIFR.rollback("#primary-nav ul li#li-models > span");
                sIFR.rollback("#primary-nav ul li#li-models > p");
                primaryNavSifrAOn();
                primaryNavSifrPOn();
                $(myobj).removeClass("active");
                $(myobj).find(".scroll-pane").hide();
            }, 600);
            $(this).data('timeout', t);
        }
    });

    /*functions which extends search bar right part to the end of the window if resolution is higher than 998px*/
	searchBoxRight();
    function searchBoxRight() {
        var a = ($(window).width() - $("#wrapper").width()) / 2;
        if (a > 0) {
            $(".search-box-right").css({
                width: a,
                "display": "block"
            });
        }
    }

    
	$("input[type=text]").focus(function () {

        if (this.value == this.defaultValue) {
            this.value = "";
        }
    });
	
    $("input[type=text]").blur(function () {
        if (this.value == "") {
            this.value = this.defaultValue;
        }
    });

    /* function to change background image of template*/
	$("#bg_changer a").click(function () {
        var tg = $(this).attr("href");
        $("#background-img").hide();
        $("#background-img").attr("src", tg);
        $("#background-img").fadeIn("slow");
        slideBgChanger();
        return false;
    });

    $("#bg_changer-lnk").click(function () {
        $("#bg_changer").show();
        slideBgChanger();
        return false;
    });

    /*animation effect for background changer wrapper*/
	function slideBgChanger() {
        var $lefty = $("#bg_changer");
        $lefty.animate({
            left: parseInt($lefty.css('left'), 10) == 0 ? -10 - $lefty.outerWidth() : 0
        });
    }

    $(".showcase-list li>p").hide();

    /*functions for showcase page*/
	if ($('.grayscale').length > 0) {

        /* we use window load, because firstly images should be loaded. */
		$(window).load(function () {
            $(".grayscale").pixastic("desaturate");
        });
        /*function to invert images on hover and show paragraph and vica versa. */
		/*Pixastic revert and desaturate functions are used. */
		$('.showcase-list li').hover(function () {
            $(this).find('p').slideDown();
            var imgId;
            if ($.browser.msie) {
                imgId = document.getElementById($(this).find('img').attr('id'));
            } else {
                imgId = document.getElementById($(this).find('canvas').attr('id'));
            }
            Pixastic.revert(imgId);
        }, function () {
            $(this).find('p').stop(true, true).hide();
            $(this).find('img').pixastic("desaturate");
        })
    }
	
	
    $("a.fancybox").fancybox({
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

	/*function to place labels over inputs in contact page*/
	$('#contact-form label').labelOver('over');
    
});

/*function which activates slider plugin in models sub menu*/

$(function () {
    //vars
    var conveyor = $(".scroll-content ul", $(".scroll-pane")),
        item = $(".scroll-content ul li", $(".scroll-pane"));
    //set length of conveyor
    conveyor.css("width", item.length * 330);
    //330 is the width of the li item under models sub menu.
    //config
    var sliderOpts = {
        max: (item.length * 330) - parseInt($(".scroll-content", $(".scroll-pane")).css("width")),
        slide: function (e, ui) {
            conveyor.css("left", "-" + ui.value + "px");
        }
    };
    //create slider
    $("#slider").slider(sliderOpts);
});

/* plugin for labels to be placed over input fields in contact page */
jQuery.fn.labelOver = function (overClass) {
    return this.each(function () {
        var label = jQuery(this);
        var f = label.attr('for');
        if (f) {
            var input = jQuery('#' + f);
            this.hide = function () {
                label.css({
                    textIndent: -10000
                })
            }
            this.show = function () {
                if (input.val() == '') label.css({
                    textIndent: 0
                })
            }
            // handlers
            input.focus(this.hide);
            input.blur(this.show);
            label.addClass(overClass).click(function () {
                input.focus()
            });
            if (input.val() != '') this.hide();
        }
    })
}

/*function which pauses the music. Music is coming from a youtube video which is embedded to the bottom of page. function pauses youtube video too.*/
function soundOff(e) {
    $('.icon-sound').toggleClass("soundoff");
	ytplayer = document.getElementById("myytplayer");
	if ($('.icon-sound').hasClass("soundoff"))
	ytplayer.pauseVideo();
	else
	ytplayer.playVideo();
};

/*function for fullscreen effect*/
function fullScreen() {
    window.open($(location).attr('href'), "mywindow", "status=0,toolbar=0,location=0,menubar=0,resizable=1,width=" + screen.availWidth + ",height=" + screen.availHeight);
    window.close();
};

/*embedding youtube video by using swfobject*/
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/2qtg39ddNYE?enablejsapi=1&playerapiid=ytplayer&autoplay=1","mysound", "1", "1", "8", null, null, params, atts);